package aooO.crm.Service.impl;

import aooO.crm.Service.ContactsRemarkService;
import aooO.crm.Service.ContactsService;
import aooO.crm.common.Utils.DateUtils;
import aooO.crm.common.Utils.UUIDUtils;
import aooO.crm.mapper.ContactsMapper;
import aooO.crm.mapper.ContactsRemarkMapper;
import aooO.crm.mapper.CustomerMapper;
import aooO.crm.pojo.Contacts;
import aooO.crm.pojo.Customer;
import aooO.crm.pojo.FunnelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ContactsServiceImpl implements ContactsService {

    @Autowired
    ContactsMapper contactsMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    ContactsRemarkMapper contactsRemarkMapper;

    @Override
    public int insertContact(Contacts record) {
        String customerId = customerMapper.selectCustomerByName(record.getCustomerId());
        // 如果存在该用户，则将contacts中的用户名改为对应的用户id
        if (customerId != null) {
            record.setCustomerId(customerId);
        } else {
            // 不存在该用户，则新创建用户，并将contacts中的用户名改为新创建的用户id
            Customer customer = new Customer();
            customer.setOwner(record.getCreateBy());
            customer.setName(record.getCustomerId());
            customer.setId(UUIDUtils.uuid());
            customer.setCreateTime(DateUtils.FormatDateTime(new Date()));
            customer.setCreateBy(record.getCreateBy());
            customerMapper.insertCustomer(customer); // 新增用户
            record.setCustomerId(customer.getId()); // 修改联系人的用户为该用户id
        }
        int i = contactsMapper.insertContact(record);
        return i;
    }

    @Override
    public List<Contacts> selectContactsByConditionForPage(Map<String, Object> map) {
        return contactsMapper.selectContactsByConditionForPage(map);
    }

    @Override
    public int selectTotalPageOfContacts(Map<String, Object> map) {
        return contactsMapper.selectTotalPageOfContacts(map);
    }

    @Override
    public int UpdateContacts(Contacts contacts) {
        // 获取前端传来的用户对应的id（前端传来的是用户名称：contacts.getCustomerId()，而数据库需要存放该用户的id）
        String customerId = customerMapper.selectCustomerByName(contacts.getCustomerId());

        // 如果存在该用户，则将contacts中的用户名改为对应的用户id
        if (customerId != null) {
            contacts.setCustomerId(customerId);
        } else {
            // 不存在该用户，则新创建用户，并将contacts中的用户名改为新创建的用户id
            Customer customer = new Customer();
            customer.setOwner(contacts.getEditBy());
            customer.setName(contacts.getCustomerId());
            customer.setId(UUIDUtils.uuid());
            customer.setCreateTime(DateUtils.FormatDateTime(new Date()));
            customer.setCreateBy(contacts.getEditBy());
            customerMapper.insertCustomer(customer); // 新增用户
            contacts.setCustomerId(customer.getId()); // 修改联系人的用户为该用户id
        }
        return contactsMapper.UpdateContacts(contacts);
    }

    @Override
    public int deleteContactsByIds(String[] ids) {
        contactsRemarkMapper.deleteContactsRemarkByContactsId(ids);
        return contactsMapper.deleteContactsByIds(ids);
    }

    @Override
    public Contacts selectContactsById(String id) {
        return contactsMapper.selectContactsById(id);
    }

    @Override
    public Contacts selectContactsDetailById(String id) {
        return contactsMapper.selectContactsDetailById(id);
    }

    @Override
    public List<Contacts> selectContactsByFuzzyName(String name) {
        return contactsMapper.selectContactsByFuzzyName(name);
    }

    @Override
    public List<FunnelVO> queryCountOfCustomerAndContactsGroupByCustomer() {
        return contactsMapper.selectCountOfCustomerAndContactsGroupByCustomer();
    }


}
